package tuvienna.jade.table;

import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import tuvienna.jade.Constants;

/**
 * If asked about the size of the table, Table will respond with the answer
 */
public class TableSizeResponseBehaviour extends CyclicBehaviour {

	private static final long serialVersionUID = 4399417511342916807L;
	private TableAgent tableAgent;

	private MessageTemplate mt;
	
	public TableSizeResponseBehaviour(TableAgent tableAgent) {
		this.tableAgent = tableAgent;
		mt = MessageTemplate.MatchConversationId(getConversationID());
	}

	public void action() {
		ACLMessage msg = tableAgent.receive(mt);
		if (msg != null) {
			// Message received. Process it
			ACLMessage reply = msg.createReply();
			reply.setPerformative(ACLMessage.INFORM);
			reply.setContent("Size"+Constants.FIELD_DELIMITER+tableAgent.getParticipantsSize());
			tableAgent.send(reply);
		} else {
			block();
		}
	}

	protected String getConversationID() {
		return Constants.TABLE_SIZE_REQUEST;
	}
	
}
